﻿package component.fileupload.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import component.fileupload.exception.FileUploadException;

public class FileUtil {

	// 默认文件上传的大小是250M
	public static final long DEFAULT_SIZE_MAX = 250 * 1024 * 1024;

	// 上传文件超过大小的提示
	public static final String UPLOADFILE_MSG = "一次上传附件大小总和不能超过250M！";

	public static final String CHARSET = "UTF-8";

	/**
	 * 获取文件路径的斜杠
	 * 
	 * @return
	 */
	public static String getFileSeparator() {
		// 根据操作系统获取文件路径的斜杠
		// System.out.println(File.separator);
		// Properties prop = new Properties(System.getProperties());
		// String osName = prop.getProperty("os.name");
		return "/";
	}

	/**
	 * 根据文件路径获取文件名称
	 * 
	 * @param filePath
	 * @return
	 */
	public static String getFileNameFromFilePath(String filePath) {
		String fileName = "";
		if (filePath.lastIndexOf("/") != -1) {
			fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
		} else if (filePath.lastIndexOf("\\") != -1) {
			fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
		} else {// filePath是一个文件名
			fileName = filePath;

		}
		return fileName;
	}

	/**
	 * 获取文件保存的相对路径
	 * 
	 * @param flag
	 * @return
	 */
	public static String getRelativePath(String flag) {
		return "/data/test";
	}

	/**
	 * 根据当前的文件名称生成保存的名称
	 * 
	 * @param fileName
	 * @param flag
	 * @return
	 */
	public static String getSaveFileName(String fileName, String flag) {
		String prefix = fileName.substring(0, fileName.lastIndexOf("."));
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
		System.out.println("文件名 = " + prefix + ",文件类型 = " + suffix);

		return System.currentTimeMillis() + prefix + "." + suffix;
	}

	/**
	 * 将路径中的 \ 换成 / ，并且最后不要有斜杠
	 * 
	 * @param fileName
	 * @return
	 */
	public static String changeSlash(String fileName) {
		String ret = "";
		for (int i = 0; i < fileName.length(); i++) {
			char c = fileName.charAt(i);
			if (i == fileName.length() - 1 && (c == '/' || c == '\\')) {

			} else if (c == '\\') {
				ret += "/";
			} else {
				ret += c;
			}
		}
		return ret;
	}

	public static void saveFile(String filePath, InputStream in)
			throws FileUploadException {
		OutputStream out = null;
		File file = new File(filePath);
		File parent = file.getParentFile();
		if (!parent.exists())
			parent.mkdirs();
		try {
			out = new FileOutputStream(file);
			byte[] b = new byte[1024];// 1024
			int bytes;
			while ((bytes = in.read(b, 0, 1024)) != -1) {
				out.write(b, 0, bytes);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		} finally {
			try {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static void saveFile(String filePath, byte[] binArray,
			long startData, long size) throws FileUploadException {
		OutputStream out = null;
		File file = new File(filePath);
		File parent = file.getParentFile();
		if (!parent.exists())
			parent.mkdirs();
		try {
			out = new FileOutputStream(file);

			out = new FileOutputStream(file);
			out.write(binArray, (int) startData, (int) size);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void deleteFile(String path) {
		File file = new File(path);

		if (file.exists()) {
			file.delete();
			// 如果父目录为空,同时删除父目录
			File parent = file.getParentFile();
			if (parent.isDirectory()) {
				if (parent.listFiles().length == 0) {
					parent.delete();
				}
			}
		}
	}

	public static void main(String[] args) {
		System.out.println(getSaveFileName("abc.doc", null));
		// System.out.println(File.separator);
	}
}
